package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.Admin;
import com.sanjishu.enterlab.electricity.sale.mapper.AdminMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public Admin findByPhone(String phone) {
        return adminMapper.findByPhone(phone);
    }

    public Admin findByPassport(String passport){
        return adminMapper.findByPassport(passport);
    }

    public Admin insert(String phone, String password, String saler){
        Admin admin = new Admin();

        admin.setCellphone(phone);
        admin.setPassword(password);

        adminMapper.insert(admin);
        return admin;
    }

    public Response updatePassword(String phone, String password) {
        Admin admin = adminMapper.findByPhone(phone);
        if (phone != null) {
            admin.setPassword(DigestUtils.md5Hex(password));
            adminMapper.updateByPrimaryKey(admin);
            return Response.buildSuccess("密码修改成功。");
        } else {
            return Response.getError("密码修改失败，请重新尝试。");
        }
    }
}
